package com.arvato.admin01.Util;

import com.arvato.admin01.pojo.Student;

import javax.swing.text.html.Option;
import java.util.Optional;

/**
 * @author xu yuanjun
 * @version 1.0.0
 * @createTime 2022/06/16 09:48:00
 * @ClassName Optional
 * @Description TODO
 */
public class OptionalDemo {


    /*public String getUserName(Student student) throws Exception{
        if (student!=null){
            if (student.getStudent()!=null){
                Student ss = student.getStudent();
                if (ss.getUsername()!=null){
                    return ss.getUsername();
                }
            }
        }
        throw new Exception("取值错误");
    }*/
    public String getUserName(Student student) throws Exception{
        return Optional.of(student)
                .map(u->u.getStudent())
                .map(a->a.getUsername())
                .orElseThrow(()->new Exception("取值错误"));
    }

    public void getUser(){
        Student student = new Student();
        Optional.ofNullable(student).ifPresent(u->{
            getUser();
        });
    }

    public Student getUser(Student student) {
        return Optional.ofNullable(student)
                .filter(u->"zhangsan".equals(student.getUsername()))
                .orElseGet(()-> {
                    Student user1 = new Student();
                    user1.setUsername("zhangsan");
                    return user1;
                });
    }

    public String getUser(String student) {
        return Optional.ofNullable(student).filter(e->"%".equals(e)).orElse("31213");
    }

}
